package com.wangsh.forward.util;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wangsh.forward.outer.service.IClientOuterOperService;
import com.wangsh.forward.outer.service.IServerOperOuterService;
import com.wangsh.webforward.common.util.ConstatFinalUtil;

/**
 * 多线程的工具类
 * 
 * @author wangsh
 *
 */
public class ServiceCallableUtil implements Callable<Object>
{
	//操作类型
	private String operType ;
	// 存储参数的map
	private Map<String, Object> paramsMap;
	
	/**
	 * 多线程调用函数
	 */
	@Override
	public Object call() throws Exception
	{
		try
		{
			String thid = UUID.randomUUID().toString().replaceAll("-", "");
			Date createTime = new Date();
			paramsMap.put("createTime", createTime);
			paramsMap.put("thid", thid);
			//1是开始,0是停止
			paramsMap.put("status", "1");
			ConstatFinalUtil.THREAD_MAP.put(thid, paramsMap);
			
			ConstatFinalUtil.SYS_LOGGER.info(thid + "-----线程池开始-----");
			if("requestMulti".equalsIgnoreCase(operType))
			{
				/* 多次请求的方法 */
				requestMultiService();
			}else if("requestClientMulti".equalsIgnoreCase(operType))
			{
				/* 多次请求的方法 */
				requestClientMulti();
			}else if("uploadCsvFacebook".equalsIgnoreCase(operType))
			{
				/* 批量处理上传的csv文件 */
				uploadCsvFacebook();
			}else if("requestFaceBookResult".equalsIgnoreCase(operType))
			{
				/* 批量上传faceBook用户数据; */
				requestFaceBookResult();
			}
			
			//将线程移除掉
			ConstatFinalUtil.THREAD_MAP.remove(thid);
			ConstatFinalUtil.SYS_LOGGER.info(thid + "-----线程池结束-----");
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("启动多线程报错了;条件:{}", paramsMap + ";operType:" + this.operType,e);
		}
		return Long.valueOf("0");
	}
	
	/**
	 * 批量上传faceBook用户数据;
	 */
	private void requestFaceBookResult()
	{
		JSONArray dataRequestArr = (JSONArray) paramsMap.get("dataRequestArr");
		int totalNum = Integer.valueOf(paramsMap.get("totalNum") + "") ; 
		int currentNum = Integer.valueOf(paramsMap.get("currentNum") + "") ; 
		
		/* IServerOperOuterService */
		IServerOperOuterService serverOperOuterService = (IServerOperOuterService) paramsMap.get("serverOperOuterService");
		serverOperOuterService.requestFaceBookResult(totalNum, currentNum, dataRequestArr);
	}

	/**
	 * 批量处理上传的csv文件
	 */
	private void uploadCsvFacebook()
	{
		List<Map<String, String>> readFileCsv = (List<Map<String, String>>) paramsMap.get("readFileCsv");
		JSONObject uploadData =  (JSONObject) paramsMap.get("uploadData");
		IServerOperOuterService serverOperOuterService = (IServerOperOuterService) paramsMap.get("serverOperOuterService"); 
		
		/* 处理csv的上传文件 */
		serverOperOuterService.fwInterfice(readFileCsv,uploadData);
	}

	/**
	 * 客户端操作
	 */
	private void requestClientMulti()
	{
		JSONObject dataReqJSON = (JSONObject) paramsMap.get("data");
		int stRecord = Integer.valueOf(paramsMap.get("stRecord") + "") ; 
		int edRecord = Integer.valueOf(paramsMap.get("edRecord") + "") ; 
		
		/* IServerOperOuterService */
		IClientOuterOperService clientOuterOperService = (IClientOuterOperService) paramsMap.get("clientOuterOperService");
		clientOuterOperService.requestMultiThreadService(dataReqJSON, stRecord, edRecord);
	}

	/**
	 * 多次请求的方法,一个网址请求多次
	 */
	private void requestMultiService()
	{
		JSONObject dataReqJSON = (JSONObject) paramsMap.get("data");
		int stRecord = Integer.valueOf(paramsMap.get("stRecord") + "") ; 
		int edRecord = Integer.valueOf(paramsMap.get("edRecord") + "") ; 
		
		/* IServerOperOuterService */
		IServerOperOuterService serverOperOuterService = (IServerOperOuterService) paramsMap.get("serverOperOuterService");
		serverOperOuterService.requestMultiThreadService(dataReqJSON, stRecord, edRecord);
	}

	public void setParamsMap(Map<String, Object> paramsMap)
	{
		this.paramsMap = paramsMap;
	}

	public void setOperType(String operType)
	{
		this.operType = operType;
	}
}
